package com.deschen.security.core.validate.code;

import com.deschen.security.core.validate.code.exception.ValidateCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author deschen
 * @Create 2019/8/21
 * @Description 验证码处理器的判断器
 * @Since 1.0.0
 */
@Component
@Slf4j
public class ValidateCodeProcessorHolder {

    // 获取所有实现ValidateCodeProcessor接口的实例化Bean，通过key获取其值
    @Autowired
    private Map<String, ValidateCodeProcessor> validateCodeProcessorMap;


    // 通过类型获取所属类型的ValidateCodeProcessor实例化bean
    public ValidateCodeProcessor findValidateCodeProcessorByType(String type) {
        // 获取ValidateCodeProcessor的类型：如 imageValidateCodeProcessor...
        log.info("【验证码判断器】type = {}", type);
        String name = type.toLowerCase() + ValidateCodeProcessor.class.getSimpleName();
        ValidateCodeProcessor validateCodeProcessor = validateCodeProcessorMap.get(name);
        log.info("【验证码判断器】验证码处理类，validateCodeProcessor = {}", validateCodeProcessor);
        if (validateCodeProcessor == null) {
            throw new ValidateCodeException("验证码处理器不存在");
        }
        return validateCodeProcessor;
    }

    public ValidateCodeProcessor findValidateCodeProcessorByType(ValidateCodeType type) {
        return findValidateCodeProcessorByType(type.getValidateCodeType());
    }


}
